package com.oe.market.modules.base.service;

import com.oe.market.modules.base.model.bean.Account;
import com.oe.market.modules.base.model.dao.AccountDao;
import com.oe.market.modules.base.model.dto.AccountDto;
import com.oe.market.modules.base.model.mapper.AccountMapper;
import com.oe.market.modules.ele.service.EleAccountService;
import org.apache.tomcat.util.buf.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

public class AccountService {

    private static final Logger log = LoggerFactory.getLogger(AccountService.class);
    public String login(String user, String password) throws Exception {


        // 检查帐号密码
        {
            Account account = AccountDao.me.checkUser(user, password);
            if (null == account) {
                throw new Exception("帐号或密码错误");
            }
        }

        String currentSign;
        {
            // 计算sign
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            Integer currentTime = Math.toIntExact(System.currentTimeMillis() / 1000);
            String data = user + password + currentTime.toString();
            currentSign = HexUtils.toHexString(messageDigest.digest(data.getBytes("UTF-8")));

            // 更新登陆信息
            AccountDao.me.update(user, currentTime, currentSign);
        }
        return currentSign;
    }

    public Map<String, Object> register(AccountDto accountDto) {
        Map<String, Object> ret = new HashMap<>();

        AccountMapper accountMapper = new AccountMapper(accountDto);
        String checkResult = AccountDao.me.checkUser(accountDto.user);
        if (null != checkResult) {
            ret.put("error", checkResult);
            return ret;
        }
        Account account = accountMapper.getAccount();
        account.setLastLogin(1);
        String result = AccountDao.me.append(account);

        if (null != result) {
            ret.put("error",result);
            return ret;
        }

        try {
            Account account1 = AccountDao.me.checkUser(account.getUser(),account.getPassword());
            EleAccountService eleAccountService = new EleAccountService();
            if (null != eleAccountService.register(account1.getAccountId())) {
                ret.put("msg","注册成功，饿了么注册出现意外");
                return ret;
            }
            String token = login(account.getUser(),account.getPassword());
            ret.put("current_sign",token);
        } catch (Exception e) {
            ret.put("msg","注册成功，饿了么注册出现意外");
        }
        return ret;
    }


    public static Account check(String user, String currentSign) {
        try {
            return AccountDao.me.fromSign(user, currentSign);
        } catch (Exception e) {
            return null;
        }
    }

    public static Account check(Cookie[] cookies) {
        if (null == cookies)
            return null;

        String user = null;
        String current_sign = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("user")) {
                user = cookie.getValue();
            }
            if (cookie.getName().equals("current_sign")) {
                current_sign = cookie.getValue();
            }
        }
        return check(user, current_sign);
    }

    public static Account check(HttpServletRequest request) {
        return check(request.getCookies());
    }
}
